﻿namespace KuiHuaBaoDian.Services.Audio {

    internal sealed partial class KHBD_AudioService {

        private sealed class EventInstance {

            public delegate void ChangedDelegate();
            public event ChangedDelegate Changed;

            private readonly IAudioEvent m_Event;

            public IAudioEvent Event => m_Event;

            public EventInstance(IAudioEvent @event) {
                @event.Modifier.Changed += Change;
                var mixer = @event.Mixer;
                while (KHBD.Context.IsApplicationPlaying2 && mixer != null) {
                    mixer.Modifier.Changed += Change;
                    mixer = mixer.Parent;
                }
                foreach (var container in @event.Containers) {
                    container.Modifier.Changed += Change;
                }
                m_Event = @event;
            }

            private void Change() {
                Changed?.Invoke();
            }
        }
    }
}
